package com.example.core.tag;

import com.example.sys.module.mode.SysModuleInfo;
import com.example.sys.module.service.SysModuleService;
import com.example.utils.SpringUtils;
import com.example.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class XBreadcrumb extends SimpleTagSupport {

    Logger logger = LoggerFactory.getLogger(getClass());

    public SysModuleService sysModuleService;

    public SysModuleService getSysModuleService() {
        if (sysModuleService == null) {
            sysModuleService = (SysModuleService) SpringUtils.getBean(SysModuleService.class);
        }
        return sysModuleService;
    }

    @Override
    public void doTag() throws JspException, IOException {
        HttpServletRequest httpRequest = (HttpServletRequest) ((PageContext) getJspContext()).getRequest();
        JspWriter out = getJspContext().getOut();
        String entityName = (String) httpRequest.getAttribute("entityName");
        if (!StringUtils.isEmpty(entityName)) {
            SysModuleInfo sysModuleInfo = this.getSysModuleService().getSysModuleInfoBySimpleName(entityName);
            if (sysModuleInfo != null) {
                out.print("<div class=\"page-context-title\">");
                out.print(" <span class=\"layui-breadcrumb\">");
                out.print("<a href=\"/\">首页</a>");
                out.print("<a><cite>");
                out.print(sysModuleInfo.getComment());
                out.print("</cite></a>");
                out.print("</span>");
                out.print("</div>");
            }
        }
    }
}
